﻿using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using System.Net.Sockets;
using System.Text;
using TouchSocket.Core;
using TouchSocket.Sockets;
using zuJianClient.PacketDto;
using zuJianClient.Utils;
using static zuJianClient.PacketDto.Whitelist;


namespace zuJianClient
{
    internal class Program
    {
        static readonly string ManufacturerId = "d2a8b4d9d70d479a8237cc304714a370";
        static readonly string DeviceId = "62E661-1B0D1E-0000FF";
        static readonly string IpAddress = "192.168.12.2";
        static readonly int Port = 22001;

        static async Task Main(string[] args)
        {
            TouchSocket.Sockets.TcpClient tcpClient = new TouchSocket.Sockets.TcpClient();

            tcpClient.Connect($"{IpAddress}:{Port}");
            
            var waitClient = tcpClient.CreateWaitingClient(new WaitingOptions()
            {   
                FilterFunc = response => //设置用于筛选的fun委托，当返回为true时，才会响应返回
                {
                    return true;
                }
            });

            var deviceLogin = new DeviceLogin(ManufacturerId, DeviceId);
            var mainPacket = new MainPacket(843, deviceLogin.ToHexString());
            byte[] returnData = await waitClient.SendThenReturnAsync(mainPacket.ToByteArray());
            var returnPacket = new MainPacket(BitConverter.ToString(returnData).Replace("-", ""));
            if (returnPacket.Flag == 0x0)
            {
                await Console.Out.WriteLineAsync("Content长度为:" + returnPacket.Length.ToString());
                await Console.Out.WriteLineAsync("Content内容为:" + BitConverter.ToString(returnPacket.Content).Replace("-", ""));
                await Console.Out.WriteLineAsync("命令为:" + returnPacket.Command.ToString());
                await Console.Out.WriteLineAsync("会话标识为:" + BitConverter.ToString(returnPacket.SessionID).Replace("-", ""));

                //心跳包
                var mainPacket_HeartBeat = new MainPacket(65535, deviceLogin.ToHexString());
                byte[] returnData_HeartBeat = await waitClient.SendThenReturnAsync(mainPacket_HeartBeat.ToByteArray());
                var returnPacket_HeartBeat = new MainPacket(BitConverter.ToString(returnData_HeartBeat).Replace("-", ""));
                if (returnPacket_HeartBeat.Flag == 0x0) { await Console.Out.WriteLineAsync("心跳包发送成功!"); }
                else { await Console.Out.WriteLineAsync("心跳包发送失败!"); }


                //string rehexString = "74D20000E9A284E588B6E88F9CE5B7A5E4B89AE59BADEFBC88E8BDA6E997B4E4B880E38081E8BDA6E997B4E4BA8CEFBC8900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000F21A4D00E7BD97E59889E4BF8A00343430373833323030313036323530333133405E00000000000000D7DB1B00E982ACE5A9B7E5A9B70034343136323231393930303730373033323298640000000000000020224D00E591A8E590AFE8998E00343231303233313937373033313332303330B321000000000000005DE31B00E99988E5AEA3E889AF00343430353130313939303032313330303358E65C000000000000007BDD1B00E58D8EE5AE97E883BD00343430393234313936383033303935333733535700000000000000B4284D00E9AD8FE9B8BFE5BDAA00343430353132323030303036323032393136F36D00000000000000F3284D00E99BB7E698A5E7BAA2003431323832393139373230343135373631332E5B000000000000000FD70F00E9BB8EE6B189E88DA300343431323330313937373032303432353137107100000000000000F23B4D00E590B4E7919EE6B3A2003337313332333139383031323238323131369A5A00000000000000A17C1400E983ADE581A5E6968700343430363833313938333032323431363338836A00000000000000A3101C00E9BAA6E59BBDE7A5A50034343036383131393938303131363437313223550000000000000076414D00E58898E4BF8A000000003432303638333139383731323031323137326E600000000000000080954D00E58898E6B885E6988E0034333034323631393638303330373237313400580000000000000013A24D00E883A1E4BA91000000003532323432383139393830363034313232376D630000000000000018A24D00E58898E5AEB6E8BE8900343330343236313939343039303632373330C8570000000000000090A24D00E9AB98E68CAFE4BC9F003433323933303139373130353032343537583C610000000000000045FD1000E58898E585B4E7BEA400343230363232313936363033303532353336CD63000000000000001EB24D00E5BCA0E5BA86E5B3B0003434303638323139373930383136313731318D6D000000000000001BC81B00E59490E58AB2E69DBE00343332393330313937333033303133333130EA5D000000000000008AF71000E59490E5ADA6E6968700343331313231313938383033313133373736765A00000000000000BBE51A00E59490E7ABAFE7949F003433323933303139373330343236333333382375000000000000004F011100E59490E5BBBAE5869B0034333239333031393733303632363333313554530000000000000057B34D00E696B9E59BBDE58D8E003433303432353139363830393137393331338A5B000000000000005CB34D00E59490E4BBB6E7949F00343332393330313936333039323133333139306D000000000000005AE51A00E4BB98E7B4A0E6B88500343332393330313937363132323035303235FC51000000000000005CE51A00E58898E99D92E69DBE003433323933303139373431313133353237380A6E000000000000003F091100E591A8E58B89E58B8700353030323334313939323038313438383334FB6000000000000000C9D21B00E58898E69F8FE698A50034333239333031393634303230393435393667540000000000000003B54D00E99988E98791E5AE8700333632323032323030333033323234303134015100000000000000EE341700E9BB84E889B3E8BF9E00343330353234313937303132323738363834357600000000000000F1B84D00E688B4E6B7B1E4BA8E00343330353234313937313035313635363730086F00000000000000FAB84D00E688B4E58B87E5869B00343332363232313937343037323635353332C750000000000000000CB94D00E5BB96E7BBA7E5BCBA00343330353234313937343035313435323932A75A0000000000000016B94D00E688B4E6B7B1E4BA9100343330353234313937313038313035363733E5690000000000000021B94D00E58898E5AD99E8BF9B003433303532353139363930373136373731373E4F00000000000000F4341700E58898E5BCBA000000003433323632343139363730373033353133393A520000000000000056B94D00E688B4E4B8ADE4BF8A003433323632323139363930363239353637386F6C0000000000000060B94D00E58898E5AD90E6988C00343330353234313938373031313133323330B25E0000000000000068B94D00E688B4E58E9AE8B4A400343332363232313937353033303335363733D55600000000000000C4841C00E591A8E5908EE5AE9A00343330353234313937333131323635323737205A00000000000000B7E51A00E8928BE6A380E5BEB700343332393236313937343132323131313335DD58000000000000005EE51A00E99988E69CACE9A699003531323232323139373631303233363034391D700000000000000018431100E591A8E58BA4E58B8700353132323232313937343034313136303131B468000000000000008CB94D00E69FB4E7949FE889AF0034313238323931393636303131383038333971520000000000000019231100E5BCA0E7A38A00000000343132383239313939303035313337363934F5630000000000000022071100E5BDADE4B8BD00000000353030323334313939303035323838383631005700000000000000CCD21B00E5BCA0E6958F000000003431323832393139393130353033373634588A5400000000000000C73F1100E8B5B5E6A182E69E9700343532343032313938363131323630333139645200000000000000CBD21B00E5BCA0E69C9DE998B300343132383239313938353031303238303131EB5B00000000000000FD0C1100E5BCA0E5AE8FE4BAAE00343132383239313936383036313537363136FF59000000000000009FC14D00E8A281E69687E7949F00333632343330313935383130313631333133F25100000000000000CCC14D00E99988E69993E5909B00343430363833313939383033333036363431D861000000000000001BC44D00E8B4BAE4BAAEE6988E003433303532343139383830313138313137371C720000000000000026C44D00E99988E5868DE69E97003433303532343139383830333238313139382C690000000000000076ED1C00E4BD95E6B0B8E5A58E0034343136323331393733303932383133333317560000000000000047E34D00E88296E7A5A5E58D8E00343330353234313939303039323335323935B260000000000000008AE44D00E688B4E8BF90E58D8E003433303532343139373530393237353531383B760000000000000091E44D00E9BE99E58B87E58D8E00343330353237313937353130303430333158C6590000000000000095E44D00E7A88BE68898E8B4A400343332363232313937373036303635303132545C000000000000009EE44D00E4BB98E78E89E5888600343332363232313936353039313934353734745200000000000000E7E44D00E58898E5BBBAE88DA3003433303532343139363530313035303033375F6200000000000000E8E54D00E8B4BAE696B9E4BC9F003433303532343139363231313038313137379956000000000000006D8B1000E99BB7E6B189E88DA300343532343231313936383132313531383358C27600000000000000CB5F1100E983ADE5BC80E58F8B00353132323232313937313039303636303331D24F00000000000000E2EA4D00E8928BE698A5E58D8E00343332393236313938303033313731313134C26400000000000000E7EA4D00E8928BE6B5B7E5869B003433313132393139383831303136313133369C5300000000000000FA121100E8B0ADE69CAFE5BAB700353030323334313939303131323833353330316D00000000000000F4EA4D00E591A8E58BA4E7A78000353132323232313937333034313336303233265600000000000000BF161100E5BCA0E59DA4E8B68500353132393231313936363035303337393135BD5E00000000000000FBEA4D00E8928BE59BA2E58F8B0034333239323631393731313131373131333398760000000000000080421D00E8928BE698A5E5BEB700343332393236313937343035323331313131486700000000000000FFEA4D00E8928BE59BA2E5BA8600343332393236313936373130313531313732806C00000000000000C8D21B00E9AB98E58F8CE5969C003433303432353139373831303236393237337B6600000000000000D2F14D00E78E8BE59BBDE5AE8900343330343235313936333039313939323730755200000000000000DEF14D00E9BB84E79FB3E5858300343330343235313936393032323239323734E95100000000000000DFF74D00E5BCA0E5A4A7E697BA003431323832393139373030373136353235311A660000000000000073E51A00E69687E696B0E5BBBA00343532333233313937373031323132383134A66400000000000000839A1100E98293E6B5B7E58991003435303932333139383430343130313831343361000000000000005FE51A00E78E8BE5B08FE5B9B30034333035323131393731303930393338333931520000000000000046371100E99988E6B998E784B100343330353238313938343039313231303935F96500000000000000C4E51A00E99988E59DA4000000003433303532383230303530313232313035369556000000000000006FFB4D00E79FB3E58DABE69E970034333132323331393932313132383336313933590000000000000057701900E9BB84E5BAB7E790BC003433323432373139373530323130333432383D5E000000000000007E6F1900E99988E7A4BCE8858A00343332343237313937333132303233313238AF6100000000000000B7054E00E4BD95E59BBDE58D8E003434303638333139393230333034333031369C5300000000000000B8054E00E590B4E69886E6B38900343430363833313939373035303233393132D2510000000000000042111D00E9BB84E994A6E5AE8100343532353232313937303132303535383338EA5C00000000000000B2BA4D00E58898E5BBBAE696B000343330343236313937323132323734333934835200000000000000781A4E00E688B4E680A1E9BE9900343330353234313936343131303635353138297100000000000000A31A4E00E688B4E6B19FE6B5B700343330353234313938323034323235363936E65A00000000000000A81A4E00E9BE99E697ADE6988E00343330353237313938343132333030333131DE7600000000000000B01A4E00E69BBEE5BF97E5AE8F00343330353234313937343038313735363733177600000000000000A1384E00E5BCA0E5ADA6E9948B003431323832393139373630313033373631350463000000000000006B454E00E688B4E58E9AE8BF9B003433303532343139363831313034353731393A5E000000000000006E454E00E998B3E58D87E587A10034333236323231393635313231373337333178580000000000000073454E00E58898E5B08FE4BA9100343332363232313936353031323333373658F65C0000000000000092714D00E5AE8BE889AFE79B8A00343330353234313938363035313235323930D76C000000000000005AA11800E4BC8DE4B887E5889A003531303332323139373730393230373831359267000000000000007D664E00E883A1E4BA94E8B4B500343330353234313937343034323434353731745700000000000000C3F91100E591A8E98193E59BBD003531323232323139373031313036373237315C5700000000000000D38B1300E58DBFE782B9E5B9B3003433323632343139363730333033303331348C6500000000000000D3"; // 从服务器获取的十六进制字符串
                //Whitelist whitelist = PacketParser.ParseWhitelistFromHex(rehexString);
                //await Console.Out.WriteLineAsync(JsonConvert.SerializeObject(whitelist));

                //取人员特征信息
                if (true)
                {
                    var personnelInfoCmd = new PersonnelInfoCmd()
                    {
                        DeviceId = DeviceId,
                        IdentityCard = "440783200106250313"
                    };
                    var mainPacket_PersonnelInfo = new MainPacket(845, personnelInfoCmd.ToHexString());


                    byte[] returnData_PersonnelInfo = await waitClient.SendThenReturnAsync(mainPacket_PersonnelInfo.ToByteArray());
                    await Console.Out.WriteLineAsync(BitConverter.ToString(returnData_PersonnelInfo).Replace("-", ""));

                    Console.WriteLine("长度:" + BitConverter.ToString(returnData_PersonnelInfo).Replace("-", "").Length.ToString());
                    var returnPacket_PersonnelInfo = new MainPacket(BitConverter.ToString(returnData_PersonnelInfo).Replace("-", ""));

                    if (returnPacket_PersonnelInfo.Flag == 0x0)
                    {
                        await Console.Out.WriteLineAsync("Content长度为:" + returnPacket_PersonnelInfo.Length.ToString());
                        await Console.Out.WriteLineAsync("Content内容为:" + BitConverter.ToString(returnPacket_PersonnelInfo.Content).Replace("-", ""));
                        await Console.Out.WriteLineAsync("命令为:" + returnPacket_PersonnelInfo.Command.ToString());
                        await Console.Out.WriteLineAsync("会话标识为:" + BitConverter.ToString(returnPacket_PersonnelInfo.SessionID).Replace("-", ""));
                    }
                    else
                    {
                        await Console.Out.WriteLineAsync("获取人员特征信息失败!");
                    }

                }


                while (false)
                {
                    //全量获取白名单
                    var mainPacket_WhiteList = new MainPacket(849, deviceLogin.ToHexString());
                    byte[] returnData_WhiteList = await waitClient.SendThenReturnAsync(mainPacket_WhiteList.ToByteArray());
                    var returnPacket_WhiteListt = new MainPacket(BitConverter.ToString(returnData_WhiteList).Replace("-", ""));
                    if (returnPacket_WhiteListt.Flag == 0x0)
                    {
                        var hexString = BitConverter.ToString(returnPacket_WhiteListt.Content).Replace("-", "");
                        await Console.Out.WriteLineAsync("白名单Content!");
                        await Console.Out.WriteLineAsync("Content长度为:" + returnPacket_WhiteListt.Length.ToString());
                        await Console.Out.WriteLineAsync("Content内容为:" + hexString);
                        //解包
                        Whitelist Upack = PacketParser.ParseWhitelistFromHex(hexString);
                        await Console.Out.WriteLineAsync(JsonConvert.SerializeObject(Upack));
                        await Console.Out.WriteLineAsync(Upack.Records.Count.ToString());
                        //退出while循环
                        break;
                    }
                    else
                    {
                        var hexString = BitConverter.ToString(returnPacket_WhiteListt.Content).Replace("-", "");
                        await Console.Out.WriteLineAsync("获取白名单失败!");
                        await Console.Out.WriteLineAsync("Content内容为:" + hexString);
                        //hexString转UTF8
                        await Console.Out.WriteLineAsync("Content内容UTF-8为:" + Encoding.UTF8.GetString(StringToByteArray(hexString)));
                    }

                    //延时10s
                    await Task.Delay(60000);
                }


            }
            else
            {
                await Console.Out.WriteLineAsync("登录失败!");
            }

        }
    }
}
